package com.my.blog.website.service.impl;

import com.my.blog.website.dao.CnbsUserRoleVoMapper;
import com.my.blog.website.dao.CnbsUserVoMapper;
import com.my.blog.website.exception.TipException;
import com.my.blog.website.modal.Vo.CnbsUserRoleVo;
import com.my.blog.website.modal.Vo.CnbsUserVo;
import com.my.blog.website.modal.Vo.CnbsUserVoExample;
import com.my.blog.website.service.ICnbsUserRoleService;
import com.my.blog.website.service.ICnbsUserService;
import com.my.blog.website.utils.TaleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author ：gyh
 * @date ：Created in 2020/8/15 22:32
 * @description：前台用户service
 * @modified By：gyh
 * @version: 1.0
 */
@Service
public class CnbsUserServiceImpl implements ICnbsUserService, UserDetailsService {


    @Autowired
    ICnbsUserService userService;

    @Resource
    CnbsUserRoleVoMapper roleMapper;

    @Autowired
    HttpSession session;

    @Resource
    CnbsUserVoMapper cnbsUserVoMapper;


    // 用户登录逻辑和验证处理
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 通过用户名查询用户
        CnbsUserVo user = userService.getUserInfoByUserName(username);

        // 放入session
        session.setAttribute("loginUser", user);

        //创建一个新的UserDetails对象，最后验证登陆的需要
        UserDetails userDetails = null;
        if (user != null) {
            System.out.println("未加密："+user.getPassword());
//            String password = TaleUtils.MD5encode(username + user.getPassword());
            // 登录后会将登录密码进行加密，然后比对数据库中的密码，数据库密码需要加密存储！
            String password = user.getPassword();

            //创建一个集合来存放权限
            Collection<GrantedAuthority> authorities = getAuthorities(user);
            //实例化UserDetails对象
            userDetails = new org.springframework.security.core.userdetails.User(username, password,
                    true,
                    true,
                    true,
                    true, authorities);
        }
        return userDetails;
    }

    @Override
    public CnbsUserVo getUserInfoByUserName(String username) {
        CnbsUserVoExample example = new CnbsUserVoExample();
        CnbsUserVoExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        long count = cnbsUserVoMapper.countByExample(example);
        if (count < 1) {
            return null;
        }
        List<CnbsUserVo> cnbsUserVos = cnbsUserVoMapper.selectByExample(example);
        return cnbsUserVos.get(0);
    }

    @Override
    public void save(CnbsUserVo userVo) {
        cnbsUserVoMapper.insert(userVo);
    }


    /**
     * @Author: gyh
     * @Description: 获取角色信息
     * @DateTime: 2020/8/19 22:51
     * @Params: [user]
     * @Return java.util.Collection<org.springframework.security.core.GrantedAuthority>
     */
    private Collection<GrantedAuthority> getAuthorities(CnbsUserVo user) {
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
        CnbsUserRoleVo role = roleMapper.selectByPrimaryKey(user.getRoleId());
        //注意：这里每个权限前面都要加ROLE_。否在最后验证不会通过
        authList.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
        return authList;
    }
}
